#!/usr/bin/env python
#encoding=utf8
"""
@描述：测试一下职能标签能匹配到多少行业，本次使用DBUtils包
@日期：2015年01月29日 星期四

@作者：hongxingfan
"""

import sys
reload(sys)
sys.setdefaultencoding("utf8")
from  DBUtils import MySQLDB
import re

# 类区域
class ThirdInfo:
	def __init__(self,name,id):
		self.name = name
		self.id = id

# 程序入口
if __name__ == "__main__":

	selectMap = """
		SELECT siteName,NAME,j.thirdName,o.thirdId FROM jobs.otherSiteToKanZhun o
		LEFT JOIN jobs.jobclass_tag j
		ON o.thirdId = j.thirdId
		ORDER BY siteName ASC
		"""
	# 连接配置
	host = "192.168.1.251"
	user = "root"
	passwd = ""
	port = 3306
	db = "jobs"
	charset = "utf8"
	db = MySQLDB(host,user,passwd,port,charset)
	# 封装成Map<string,Map<string,string>>结构
	keyValue = {}
	keyValue_1 = {}
	# 执行sql,装载到字典中
	key = None
	param = []
	resultMap = db.select(selectMap,param)
	for (siteName,name,thirdName,thirdId) in resultMap:
		#print("%s\t%s\t%s\t%s" %(siteName,name,thirdName,thirdId))
		if key != siteName and key != None:
			keyValue[key] = keyValue_1
			keyValue_1 = {}
		if(name in keyValue_1):
			keyValue_1[name].append(ThirdInfo(thirdName,thirdId))
		else:
			keyValue_1[name] = [ThirdInfo(thirdName,thirdId)]
		key = siteName
	keyValue[key] = keyValue_1
	# 测试输出
	#for (k,v) in keyValue.items():
	#	for (kk,vv) in v.items():
	#		for vvv in vv:
	#			print("%s\t%s\t%s\t%s" %(k,kk,vvv.name,vvv.id))
	# 遍历jobs数据表，找出与keyValue中相匹配的内容
	selectJobs = "SELECT id,provider,jobclass FROM jobs.jobs WHERE id<%s AND _update_at>'2015-01-30' ORDER BY id DESC LIMIT 0,5000"
	param = [sys.maxint]
	kv = {}
	kv["zhilian_yes"] = 0
	kv["zhilian_no"] = 0
	kv["job51_yes"] = 0
	kv["job51_no"] = 0
	kv["liepin_yes"] = 0
	kv["liepin_no"] = 0
	total = 0
	while(True):
		# (id,provider,jobclass)
		result = db.select(selectJobs, param)
		index = 0
		for(id,provider,jobclass) in result:
			if(id < param[0]):
				param[0] = id
			index += 1
			total += 1
			jobclass = jobclass.replace("\"","").replace("[","").replace("]","")
			classes = re.split(",",jobclass)
			provider = provider.lower()
			if(provider not in keyValue):
				continue
			#print("%d\t%s\t" %(id,provider)),
			flag = "no"
			for c in classes:
				if c == None or c == "":
					continue;
				thirdInfoList = keyValue.get(provider).get(c)
				if(thirdInfoList is None):
				#   kv[provider + "-no"] += 1
					break
				for th in thirdInfoList:
					#print("%s\t" %(th.name)),
					flag = "yes"
					break
			kv[provider + "_" + flag] += 1
		if(index < 40):
			break
		print("Index is:%d" %(index))
	print("总数条数是：%d" %(total))
	for (k,v) in kv.items():
		print("%s\t%d" %(k,v))
	db.close()
